package com.ecarx.engineermode;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.ecarx.engineermode.controller.ProtectedFeatureController;
import com.ecarx.engineermode.tab.Model.TabModel;
import com.ecarx.engineermode.tab.adapter.ExpandableListviewAdapter;
import com.ecarx.engineermode.tab.model.TabItem;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private FragmentManager supportFragmentManager;
    private List<TabItem> tabItemList;
    private TextView tvTitle;
    private ImageView ivTitle;
    private ProtectedFeatureController mProtectedFeatureController = new ProtectedFeatureController();
    private ExpandableListviewAdapter expandableListviewAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvTitle = findViewById(R.id.tv_fg_title);
        ivTitle = findViewById(R.id.iv_fg_title);
        View ivExit = findViewById(R.id.iv_exit);
        View ivTitle = findViewById(R.id.iv_tilte);

        ivExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                System.exit(0);
            }
        });

        supportFragmentManager = getSupportFragmentManager();
        initListView();
        mProtectedFeatureController.init(new ProtectedFeatureController.Callback() {
            @Override
            public void onAccessCallback() {
                TabModel.addProtectedTabItem(getApplicationContext(), tabItemList);
                expandableListviewAdapter.notifyDataSetChanged();
            }
        });

        ivTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mProtectedFeatureController.request();
            }
        });
    }

    private void initListView() {
        ExpandableListView expandableListView = findViewById(R.id.elv_tab);
        expandableListView.setGroupIndicator(null);
        expandableListView.setChildIndicator(null);
        expandableListView.setDividerHeight(0);
        tabItemList = TabModel.getTabItemList(getApplicationContext());
        expandableListviewAdapter = new ExpandableListviewAdapter(this, tabItemList);
        expandableListView.setAdapter(expandableListviewAdapter);
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                TabItem item = tabItemList.get(groupPosition);
                expandableListviewAdapter.switchSelectedView(parent, item.getGroupItem().getItemName(), "", tabItemList.get(groupPosition).isOnlyGroup());
                if (tabItemList.get(groupPosition).isOnlyGroup()) {
                    switchFragment(groupPosition, 0);
                    return true;
                }
                return false;
            }
        });
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                TabItem item = tabItemList.get(groupPosition);
                switchFragment(groupPosition, childPosition);
                expandableListviewAdapter.switchSelectedView(parent, item.getGroupItem().getItemName(),
                        item.getChildItemList().get(childPosition).getItemName(), tabItemList.get(groupPosition).isOnlyGroup());
                return true;
            }
        });
        switchFragment(0, 0);
    }

    private Fragment topFragment;

    void switchFragment(int groupPosition, int childPosition) {
        TabItem tabItem = tabItemList.get(groupPosition);
        Fragment fragment;
        Drawable icon = tabItem.getGroupItem().getIcon();
        String title;
        if (tabItem.isOnlyGroup()) {
            fragment = tabItem.getGroupItem().getFragment();
            title = tabItem.getGroupItem().getItemName();
        } else {
            fragment = tabItem.getChildItemList().get(childPosition).getFragment();
            title = tabItem.getChildItemList().get(childPosition).getItemName();
        }
        if (fragment == null) {
            return;
        }


        if (fragment == topFragment) {
            return;
        }
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        if (topFragment != null) {
            Log.d(TAG, "switchFragment: hide Fragment =" + topFragment.getClass().getSimpleName());
            fragmentTransaction.hide(topFragment);
        }

        topFragment = fragment;
        boolean contains = supportFragmentManager.getFragments().contains(fragment);
        if (!contains) {
            Log.d(TAG, "switchFragment: add show=" + fragment.getClass().getSimpleName());
            fragmentTransaction.add(R.id.fl_container, fragment, fragment.getClass().getSimpleName());
        }
        fragmentTransaction.show(fragment);
        fragmentTransaction.commitAllowingStateLoss();
        ivTitle.setImageDrawable(icon);
        tvTitle.setText(String.format(" / %s", title));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}